﻿using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace VietNamKey
{
    [DefaultEvent("SettingChanged")]
    public partial class ucGeneral : UserControl
    {
        public event EventHandler SettingChanged;
        bool isOk = false;
        public ucGeneral()
        {
            InitializeComponent();
            //if (LicenseManager.UsageMode != LicenseUsageMode.Designtime)
                Reload();
        }

        public void Reload()
        {
            isOk = false;
            lstBangMa.Items.Clear();
            lstBangMa.Items.AddRange(Encodings.List);
            lstBangMa.SelectedIndex = Global.KeySettings.Encoding;

            lstKieuGo.Items.Clear();
            lstKieuGo.Items.AddRange(TypingMethods.List);
            lstKieuGo.SelectedIndex = Global.KeySettings.TypingMethod;

            cbbPhimTat.Enabled = rdbAltZ.Checked = Global.KeySettings.UseAltZ;
            rdbCtrlShift.Checked = !Global.KeySettings.UseAltZ;
            if (Global.KeySettings.AltZKey != Keys.None)
                cbbPhimTat.SelectedIndex = ((int)Global.KeySettings.AltZKey) - 65;
            else
                cbbPhimTat.SelectedIndex = 0;

            isOk = true;
        }

        private void RaiseEvent(object sender, EventArgs e)
        {
            if (!isOk) return;
            if (sender == rdbAltZ)
                cbbPhimTat.Enabled = rdbAltZ.Checked;

            Global.KeySettings.UseAltZ = rdbAltZ.Checked;
            Global.KeySettings.AltZKey = rdbAltZ.Checked ? (Keys)(cbbPhimTat.SelectedIndex + 65) : Keys.None;
            Global.KeySettings.TypingMethod = lstKieuGo.SelectedIndex;
            Global.KeySettings.Encoding = lstBangMa.SelectedIndex;

            if (SettingChanged != null) SettingChanged(this, new EventArgs());
        }
    }
}
